package com.boxin.framework.base.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


public class DateUtils {

	/**
	 * <p>
	 * Author:Jakemanse
	 * <p>
	 * <p>
	 * time: 2012-9-11 下午6:00:44
	 * <p>
	 * <p>
	 * function: 返回时间格式为：yyyymmdd 如：20120911
	 * <p>
	 * <p>
	 * @return
	 * <p>
	 */
	public static String getLastDayString() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = calendar.getTime();
		return format.format(date).toString();
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-5-29  下午2:32:11
	 * @function <p> 返回N天前的日期   返回时间格式为：yyyymmdd 如：20120911  </p>
	 * @param day
	 * @return
	 */
	
	public static String getBeforeDaysString(int day){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -day);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = calendar.getTime();
		return format.format(date).toString();
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-28  下午1:55:30
	 * @function <p> 获取延迟N天后的 日期时间 </p>
	 * @param days
	 * @return
	 */
	public static Date getDelayDate(int days){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, days);
		Date date = calendar.getTime();
		date.setHours(23);
		date.setMinutes(59);
		date.setSeconds(59);
		
		return date;
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-5-29  下午2:32:11
	 * @function <p> 返回N天后的日期   返回时间格式为：yyyymmdd 如：20120911  </p>
	 * @param day
	 * @return
	 */
	public static String getAfterDaysString(int day){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, day);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = calendar.getTime();
		return format.format(date).toString();
	}
	
	
	
	
	public static String getTomorrowString(){
		return getAfterDaysString(1);
	}
	
	public static String getTodayString() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = calendar.getTime();
		return format.format(date).toString();
	}

	
	
	public static String getTodayStringByFormat(String format){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		Date date = calendar.getTime();
		return fmt.format(date).toString();
	}



	public static String getMonthString() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("MM");
		Date date = calendar.getTime();
		return format.format(date).toString();
	}

	public static String getYearString() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy");
		Date date = calendar.getTime();
		return format.format(date).toString();
	}
	public static String getYearMonthString(){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
		Date date = calendar.getTime();
		return format.format(date).toString();
	}

	public static String getDayString() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("dd");
		Date date = calendar.getTime();
		return format.format(date).toString();
	}

	public static String getNormalDateString() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = calendar.getTime();
		return format.format(date).toString();

	}
	
	public static String getNormalDateString(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date).toString();
	}


	/**
	 * * 把字符串转化为日期类型 ("2000/12-3号 10时02:3") * * @param date * @return * @throws
	 * ParseException
	 * @throws ParseException 
	 */
	public static Date parseStrToDate(String date) throws ParseException  {
		Date result = null;
		String parse = date.trim();
		parse = parse.replaceFirst("^[0-9]{4}([^0-9]?)", "yyyy$1");
		parse = parse.replaceFirst("^[0-9]{2}([^0-9]?)", "yy$1");
		parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1MM$2");
		parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}( ?)", "$1dd$2");
		parse = parse.replaceFirst("( )[0-9]{1,2}([^0-9]?)", "$1HH$2");
		parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1mm$2");
		parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1ss$2");
		DateFormat format = new SimpleDateFormat(parse);
		result = (Date) format.parse(date);
		return result;
	}
	
	
	
	public static int getDiffDaysTo(Date startDate,Date endDate){
		long startMill = startDate.getTime();
		long endMill = endDate.getTime();
		
		long diffn = (endMill-startMill)/(1000*60*60*24);
		
		return (int) diffn;
	}
	
	public static Integer getDiffHours(Date startDate,Date endDate){
		long startMill = startDate.getTime();
		long endMill = endDate.getTime();
		long houth = (endMill-startMill)/(60 * 60 * 1000);
		return (int) houth;
	}
	
	
	
	public static List<String> getCurrentMonthDays(){
		Calendar calendar =  Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) +1;
		int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		//int day = calendar.get(Calendar.DAY_OF_MONTH);
		
		List<String> dayList = new ArrayList<String>();
		for(int i=1;i<=day;i++){
			StringBuffer strbffer = new StringBuffer();
			String strmonth = (month <10) ? "0"+month:month+"";
			String strday = (i <10) ? "0"+i:i+"";
			strbffer.append(year).append(strmonth).append(strday);
			dayList.add(strbffer.toString());
		}
		return dayList;
		
	}
	/**
	 * 
	 * @author zhuxingang
	 * @time 2013-6-1 下午04:55:17
	 * @function <p>获取指定月天数 yyyymmdd</p>
	 * @param month
	 * @return
	 */
	public static List<String> getMonthDays(int month){
		Calendar calendar =  Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int day = -1;
		if(month==2){
			
			if(year % 4 != 0 || year % 100 == 0 && year % 400 != 0){
				
				day = 28;
				
			}else{
				
				day = 29;
				
			}
			
		}else{
			
			if(month==4 || month==6 || month==9 || month==11){
				
				day=30;
				
			}else{
				
				day=31;
			}
		}
		List<String> dayList = new ArrayList<String>();
		for(int i=1;i<=day;i++){
			
			StringBuffer strbffer = new StringBuffer();
			String strmonth = (month <10) ? "0"+month:month+"";
			String strday = (i <10) ? "0"+i:i+"";
			strbffer.append(year).append(strmonth).append(strday);
			dayList.add(strbffer.toString());
			
		}
		return dayList;
	}
	/**
	 * 获取本年度月字符串 yyyymm
	 * @author zhuxingang
	 * @return
	 */
	public static List<String> getYearMonthList(){
		
		Calendar calendar =  Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		List<String> YearMonthList = new ArrayList<String>();
		YearMonthList.add("");
		for(int i=1;i<=12;i++){
			StringBuffer strbffer = new StringBuffer();
			if(i>9){
				strbffer.append(year).append(i);
			}else{
				strbffer.append(year).append("0").append(i);
			}
			YearMonthList.add(strbffer.toString());
		}
		
		return YearMonthList;
	}
	/**
	 * 获得当前月的上个月      格式：yyyymm
	 * @author zhuxingang
	 * @return
	 */
	public static String getPreMonth(){
		//获得当前月
		String currentMonth = getMonthString();
		Calendar calendar =  Calendar.getInstance();
		Integer year = calendar.get(Calendar.YEAR);
		String preMonth = "";
		if(currentMonth.equals("01")){
			
			preMonth = (year-1)+"12";
			
		}else if("0".equals(currentMonth.substring(0, 1))){
			
			preMonth = year.toString()+"0"+ (Integer.parseInt(currentMonth.substring(1))-1);
			
		}else if("10".equals(currentMonth)){
			
			preMonth = year.toString() +"0"+ (Integer.parseInt(currentMonth)-1);
			
		}else{
			
			preMonth = year.toString()+ (Integer.parseInt(currentMonth)-1);
			
		}
		return preMonth;
	}
	/**
	 * 
	 * @author zhuxingang
	 * @time 2013-7-31 上午11:42:34
	 * @function <p>获取当前日期的上周一，周日</p>
	 * @return
	 */
	public static Map<String,Object> getLastWeekDays(){
		
		Calendar calendar1 = Calendar.getInstance();//last Monday
		Calendar calendar2 = Calendar.getInstance();//last Sunday
		Map<String,Object> params = new HashMap<String,Object>();
		int dayOfWeek=calendar1.get(Calendar.DAY_OF_WEEK)-1;
		int offset1=1-dayOfWeek;
		int offset2=7-dayOfWeek;
		calendar1.add(Calendar.DATE, offset1-7);
		calendar2.add(Calendar.DATE, offset2-7);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		params.put("lastMonday", format.format(calendar1.getTime()).toString());
		params.put("lastSunday", format.format(calendar2.getTime()).toString());
		return params;
		
	}
	/**
	 * 
	 * @author zhuxingang
	 * @time 2013-7-31 下午01:06:30
	 * @function <p>获取当前日期的周一，周日</p>
	 * @return
	 */
	public static Map<String,Object> getCurrWeekDays(){
		
		Map<String,Object> params = new HashMap<String,Object>();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar currMonday = Calendar.getInstance();//current Monday
		Calendar currSunday = Calendar.getInstance();//current Sunday
		currMonday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		currSunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
      //取本周第一天 最后一天  
		params.put("currMonday", format.format(currMonday.getTime()).toString());
		params.put("currSunday", format.format(currMonday.getTime().getTime()+(6 * 24*60*60*1000)).toString());
		return params;
	}
	/**
	 * 
	 * @author zhuxingang
	 * @time 2013-7-31 下午01:11:44
	 * @function <p>获取当前月第一天,最后一天</p>
	 * @return
	 */
	public static Map<String,Object> getCurrMonthBeginEnd(){
        
		Map<String,Object> params = new HashMap<String,Object>();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();    
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String currBeginMonth = format.format(c.getTime());
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();    
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
        String currEndMonth = format.format(ca.getTime());
        params.put("currBeginMonth", currBeginMonth);
        params.put("currEndMonth", currEndMonth);
        return params;
        
	}
	/**
	 * 
	 * @author zhuxingang
	 * @time 2013-7-31 下午01:11:44
	 * @function <p>获取当前月的上月第一天,最后一天</p>
	 * @return
	 */
	public static Map<String,Object> getpreMonthBeginEnd(){
		
		Map<String,Object> params = new HashMap<String,Object>();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		//获取前月的第一天
        Calendar   cal_1=Calendar.getInstance();//获取当前日期 
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String preBeginMonth = format.format(cal_1.getTime());
        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();   
        cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天 
        String preEndMonth = format.format(cale.getTime());
        params.put("preBeginMonth", preBeginMonth);
        params.put("preMonthEnd", preEndMonth);
        return params;
	}
	
	public static String getDateString(Long longdate){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(longdate);
		return format.format(date).toString();
	}
	
	
	public static String formatDate(Date date,String strformat){
		SimpleDateFormat format = new SimpleDateFormat(strformat);
		return format.format(date).toString();
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-15  下午11:11:30
	 * @function <p> 获取当前天的起始时间 如  2013-10-12 00：00：00 </p>
	 * @return
	 */
	public static Date getDayStartTime(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	public static void main(String[] args) {
		
		Date date2 = getDelayDate(5);
		System.out.println(date2.toString());
		
		String a = getBeforeDaysString(5);
		getMonthDays(5);
		
		getCurrentMonthDays();
		
		String myString = "2013/05/08";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
		try {
			
			
			
			Date date = sdf.parse(myString);
			Date nowDate = new Date();
			int n = nowDate.compareTo(date);
			
			long start = date.getTime();
			long end = nowDate.getTime();
			
			long diffn = (end-start)/(1000*60*60*24);
			 
			 System.out.println("相差："+diffn);
			
			 System.out.println("方法--->"+getDiffHours(date, new Date()));
			 
			 
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
	}

}
